//
//  LikeViewController.m
//  YouGoWeGo
//
//  Created by Dai on 14/11/19.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "LikeViewController.h"
#import "LIkeDetailViewController.h"
#import "Database.h"
#import "AreaModel.h"
#import "UIImageView+WebCache.h"

@interface LikeViewController ()

@end

@implementation LikeViewController

//- (void)dealloc
//{
//    [_table release];
//    [_tableArray release];
//    [_indexPath release];
//    [_image release];
//    [super dealloc];
//}
//- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        
//        self.tableArray = [NSMutableArray array];
////        self.tableArray = [Database selectAll:@"Picture"];
//        
//    }
//    return self;
//}
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    [self.view setBackgroundColor:[UIColor whiteColor]];
//    self.navigationController.navigationBar.translucent = NO;
//    [self.navigationItem setTitle:@"喜欢"];
//    [self creatTable];
//}
//
//#pragma mark -
//#pragma TableView
//- (void)creatTable
//{
//    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
//    [_table setBackgroundColor:[UIColor whiteColor]];
//    _table.dataSource = self;
//    _table.delegate = self;
//    [self.view addSubview:_table];
//    _table.rowHeight = 200;
//    [_table release];
//
//}
//#pragma mark -
//#pragma TableViewCell
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *cellIdentify = @"cellIdentify";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
//    if (!cell) {
//        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
//        
//    }
//    
//    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, 180)];
//    [_image setBackgroundColor:[UIColor yellowColor]];
//    [cell addSubview:_image];
//    [_image release];
//    return cell;
//
//}
//#pragma mark -
//#pragma row数量
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return [_tableArray count];
//
//}
//#pragma mark -
//#pragma 跳转
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//     //被取消选中状体
//    self.indexPath = indexPath;
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    LIkeDetailViewController *detail = [[LIkeDetailViewController alloc] init];
//    [self.navigationController pushViewController:detail animated:YES];
//    [detail release];
//
//}

- (void)dealloc
{
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog(@"lll");
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.likeArray = [NSMutableArray array];
//        self.likeArray = [NSMutableArray arrayWithObjects:@"0.png", @"1.png", @"2.png", @"4.png", @"5.png", @"4.png", @"0.png", @"1.png", @"2.png", @"5.png", @"4.png", @"2.png", @"4.png", @"5.png", @"4.png", @"5.png", @"4.png", @"0.png", nil];
//        self.likeArray = [NSMutableArray array];
        
        
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"ddd");
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
    NSMutableArray *arr = [Database selectAll:@"Picture"];
    for (Model *model in arr) {
        
        NSString *str = model.Data;
        NSString *pInfo = model.Title;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:str forKey:@"url"];
        [dic setObject:pInfo forKey:@"pInfo"];
        
//        NSMutableArray *arr = [NSMutableArray arrayWithObjects:dic, nil];
        [self.likeArray addObject:dic];
        
        
    }
    
    
    [self createScroll];
    [self createView];
    
    
}

- (void)createScroll
{
    self.likeScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    [_likeScroll setScrollEnabled:YES];
    [_likeScroll setShowsVerticalScrollIndicator:YES];
    [_likeScroll setContentSize:CGSizeMake(0, self.view.frame.size.height * 2)];
    
    [self.view addSubview:_likeScroll];
    [_likeScroll release];
}

- (void)createView
{
    UIView *fView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0)];
    UIView *sView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0)];
    UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0)];
//    UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
//    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0.png"]];
//    UIImageView *image3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"3.png"]];
//    
//    CGSize size1 = image1.image.size;
//    CGFloat height1 = size1.height * WIDTH / size1.width;
//    
//    CGSize size2 = image2.image.size;
//    CGFloat height2 = size2.height * WIDTH / size2.width;
//    
//    CGSize size3 = image3.image.size;
//    CGFloat height3 = size3.height * WIDTH / size3.width;
//    
//    image1.frame = CGRectMake(0, 0, WIDTH, height1);
//    image2.frame = CGRectMake(WIDTH, 0, WIDTH, height2);
//    [image2 setBackgroundColor:[UIColor redColor]];
//    image3.frame = CGRectMake(WIDTH * 2, 0, WIDTH, height3);
//
    CGFloat fHeight = fView.frame.size.height;
    CGFloat sHeight = sView.frame.size.height;
    CGFloat tHeight = tView.frame.size.height;
//
//    
//    [fView addSubview:image1];
//    [sView addSubview:image2];
//    [tView addSubview:image3];
//    //
//    [self.view addSubview:fView];
//    [self.view addSubview:sView];
//    [self.view addSubview:tView];

    
    for (int i = 0; i < [_likeArray count]; i ++) {
        
        NSMutableDictionary *dic = [_likeArray objectAtIndex:i];
        NSString *str = [dic objectForKey:@"url"];
        NSString *pInfo = [dic objectForKey:@"pInfo"];
        CGFloat image_h = [[pInfo substringWithRange:NSMakeRange(0, 3)] floatValue];
        CGFloat image_w = [[pInfo substringWithRange:NSMakeRange(3, 3)] floatValue];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image_w, image_h)];

        
//        CGSize size = image.image.size;
        CGFloat h = image_h * WIDTH / image_w;
        [image setImageWithURL:[NSURL URLWithString:str]];

        NSInteger flag = 0;
        
        // 三个view相同高度时,图片添加到第一个view
        if (fHeight == sHeight && sHeight == tHeight) {
            fHeight += h;
            [image setFrame:CGRectMake(0, fHeight - h, WIDTH, h)];
            fView.frame = CGRectMake(0, 0, WIDTH, fHeight);
            [fView addSubview:image];
            [self.likeScroll addSubview:fView];
            continue;
        }
        
        // 后面两个view高度想同时,图片添加到第二个view
        if (fHeight != sHeight && sHeight == tHeight) {
            sHeight += h;
            [image setFrame:CGRectMake(WIDTH, sHeight - h, WIDTH, h)];
            sView.frame = CGRectMake(0, 0, WIDTH, sHeight);
            [sView addSubview:image];
            [self.likeScroll addSubview:sView];
            continue;
        }
        // 第一个和第三个view高度想同时, 图片添加到第一个view
        if (fHeight == sHeight & sHeight != tHeight) {
            fHeight += h;
            [image setFrame:CGRectMake(0, fHeight - h, WIDTH, h)];
            fView.frame = CGRectMake(0, 0, WIDTH, fHeight);
            [fView addSubview:image];
            [self.likeScroll addSubview:fView];
            continue;
        }
        
        // 如果第一个view高度最低,添加到fview
        if ((fHeight < sHeight && fHeight < tHeight)/* || (fHeight == sHeight && sHeight == tHeight)*/) {
            fHeight += h;
            [image setFrame:CGRectMake(0, fHeight - h, WIDTH, h)];
            fView.frame = CGRectMake(0, 0, WIDTH, fHeight);
            [fView addSubview:image];
            [self.likeScroll addSubview:fView];
            flag = 1;
        }
        
        // 添加到sview
        if (((sHeight < fHeight && sHeight < tHeight) && flag == 0 )) {
            sHeight += h;
            [image setFrame:CGRectMake(WIDTH, sHeight - h, WIDTH, h)];
            sView.frame = CGRectMake(0, 0, WIDTH, sHeight);
            [sView addSubview:image];
            [self.likeScroll addSubview:sView];
            flag = 1;
        }
        
        // 添加到tview
        if (((tHeight < fHeight && tHeight < sHeight) && flag == 0)/* || (fHeight == tHeight)*/) {
            tHeight += h;
            [image setFrame:CGRectMake(WIDTH * 2, tHeight - h, WIDTH, h)];
            tView.frame = CGRectMake(0, 0, WIDTH, tHeight);
            [tView addSubview:image];
            [self.likeScroll addSubview:tView];
        }
    }

    
}



@end
